package com.relytosoft.mizar.media.display.services.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.relytosoft.dubhe.common.satoken.entity.CurrentUser;
import com.relytosoft.mizar.media.display.api.entity.VideoConfig;
import com.relytosoft.mizar.media.display.services.mapper.VideoConfigMapper;
import com.relytosoft.mizar.media.display.services.service.VideoConfigService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 视频预览配置业务类
 *
 * @author hj
 * @version 1.0
 * @date 2022/4/13 16:00
 * @ClassName VideoConfigServiceImpl
 */
@Service
public class VideoConfigServiceImpl extends ServiceImpl<VideoConfigMapper, VideoConfig> implements VideoConfigService {
    /**
     * 通过用户id查询当前配置信息
     * @return com.relytosoft.mizar.media.display.api.entity.VideoConfig
     * @author jgg
     * @date 2022/4/13 15:37
     */
    @Override
    public VideoConfig getByUserId() {
        int currentUserId = CurrentUser.getCurrentUserId();
        LambdaQueryWrapper<VideoConfig> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(VideoConfig::getUserId,currentUserId);
        VideoConfig one = getOne(wrapper);
        if(Objects.isNull(one)){
            one = new VideoConfig();
        }
        return one;
    }


    /**
     * 设置视频预览配置
     *
     * @param config
     * @return boolean
     * @author jgg
     * @date 2022/4/13 15:51
     */
    @Override
    public boolean edit(VideoConfig config) {
        int currentUserId = CurrentUser.getCurrentUserId();
        VideoConfig selectConfig = getByUserId();
        //设置当前userId
        config.setUserId(currentUserId);
        if(Objects.isNull(selectConfig.getId())){
            return save(config);
        }
        config.setId(selectConfig.getId());
        return updateById(config);
    }
}
